package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipRepairStep;
import cc.comac.mom.bean.QualityAttachmentGroup;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipRepairStepDao;
import cc.comac.mom.entity.EquipRepairStepEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipRepairStepDataHandler extends ServiceImpl<EquipRepairStepDao, EquipRepairStepEntity> {

	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
	private EquipRepairStepDao equipRepairStepDao;

	@Autowired
	private QualityAttachmentGroupDataHandler qualityAttachmentGroupDataHandler;

	public ResultBean insertBatch(List<EquipRepairStep> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<EquipRepairStepEntity> listEntity = new ArrayList<EquipRepairStepEntity>();
		for (EquipRepairStep item : list) {
			EquipRepairStepEntity innerData = item.getInnerData();
			if (innerData == null) {
				throw new RuntimeException("数据为空");
			}
			if (item.getInnerData().getRepairId() == 0) {
				throw new RuntimeException("维修信息主键不存在");
			}
			listEntity.add(innerData);

		}
		QueryWrapper<EquipRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairStepEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("repair_id", list.get(0).getInnerData().getRepairId());
		queryWrapper.orderByDesc("step_sequence");
		List<EquipRepairStepEntity> selectList = equipRepairStepDao.selectList(queryWrapper);
		if(selectList == null || selectList.size()<1) {
			for(int i =0;i<listEntity.size();i++) {
				baseEntityDataUtil.setBaseEntityData(listEntity.get(i));
				listEntity.get(i).setStepSequence(i);
			}
		}else {
			int stepSequence = selectList.get(0).getStepSequence()+1;
			for(int i =stepSequence;i<listEntity.size()+stepSequence;i++){
				baseEntityDataUtil.setBaseEntityData(listEntity.get(i-stepSequence));
				listEntity.get(i-stepSequence).setStepSequence(i);
			}
		}
		
		boolean saveBatch = this.saveBatch(listEntity);
		if (!saveBatch) {
			throw new RuntimeException("保存维修步骤失败");
		}
		return resultBean;
	}

	// 修改
	public ResultBean update(EquipRepairStep obj) {
		ResultBean resultBean = new ResultBean(true);
		if (obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		if (StringUtils.isEmpty(obj.getInnerData().getRepairItem())) {
			throw new RuntimeException("维修项不能为空");
		}
		if (obj.getInnerData().getRepairId() == 0) {
			throw new RuntimeException("维修信息主键不存在");
		}
		baseEntityDataUtil.updateBaseEntityData(obj.getInnerData());
		int updateById = equipRepairStepDao.updateById(obj.getInnerData());
		if (updateById < 1) {
			throw new RuntimeException("修改维修项失败");
		}
		return resultBean;
	}

	// 删除
	public ResultBean deleteBatch(List<EquipRepairStep> list) {
		ResultBean resultBean = new ResultBean(true);
		if (list == null || list.size() < 1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		for (EquipRepairStep item : list) {
			EquipRepairStepEntity innerData = item.getInnerData();
			if (innerData == null) {
				throw new RuntimeException("数据为空");
			}
			listIds.add(innerData.getId());
		}
		List<EquipRepairStepEntity> selectBatchIds = equipRepairStepDao.selectBatchIds(listIds);
		for (EquipRepairStepEntity item : selectBatchIds) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean updateBatchById = this.updateBatchById(selectBatchIds);
		if (!updateBatchById) {
			throw new RuntimeException("批量删除维修步骤失败");
		}
		return resultBean;
	}

	// 上移
	public ResultBean moveUp(EquipRepairStep obj) {
		ResultBean resultBean = new ResultBean(true);
		// 校验数据
		if (obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairStepEntity innerData = obj.getInnerData();

		EquipRepairStepEntity selectById = equipRepairStepDao.selectById(innerData.getId());
		if (selectById == null) {
			throw new RuntimeException("上移数据为空");
		}
		QueryWrapper<EquipRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairStepEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("repair_id", selectById.getRepairId());
		queryWrapper.lt("step_sequence", selectById.getStepSequence());
		queryWrapper.orderByDesc("step_sequence");
		Page<EquipRepairStepEntity> page = new Page<>(0, 1, false);
		IPage<EquipRepairStepEntity> iPage = equipRepairStepDao.selectPage(page, queryWrapper);
		List<EquipRepairStepEntity> list = iPage.getRecords();
		if (list == null || list.size() < 1) {
			throw new RuntimeException("已经是第一条数据，无法上移");
		}
		EquipRepairStepEntity equipRepairStepEntity = list.get(0);

		int temp = selectById.getStepSequence();
		selectById.setStepSequence(equipRepairStepEntity.getStepSequence());
		equipRepairStepEntity.setStepSequence(temp);
		baseEntityDataUtil.updateBaseEntityData(selectById);
		baseEntityDataUtil.updateBaseEntityData(equipRepairStepEntity);

		ArrayList<EquipRepairStepEntity> arrayList = new ArrayList<EquipRepairStepEntity>();
		arrayList.add(equipRepairStepEntity);
		arrayList.add(selectById);
		boolean batchById = updateBatchById(arrayList);

		if (!batchById) {
			throw new RuntimeException("上移失败");
		}
		return resultBean;
	}
//下移

	public ResultBean moveDown(EquipRepairStep obj) {
		ResultBean resultBean = new ResultBean(true);
		// 校验数据
		if (obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairStepEntity innerData = obj.getInnerData();
		if (innerData == null) {
			throw new RuntimeException("数据为空");
		}
		EquipRepairStepEntity selectById = equipRepairStepDao.selectById(innerData.getId());
		if (selectById == null) {
			throw new RuntimeException("下移数据为空");
		}
		QueryWrapper<EquipRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairStepEntity>();
		queryWrapper.eq("row_status", 1);
		queryWrapper.eq("repair_id", selectById.getRepairId());
		queryWrapper.gt("step_sequence", selectById.getStepSequence());
		queryWrapper.orderByAsc("step_sequence");
		Page<EquipRepairStepEntity> page = new Page<>(0, 1, false);
		IPage<EquipRepairStepEntity> iPage = equipRepairStepDao.selectPage(page, queryWrapper);
		List<EquipRepairStepEntity> list = iPage.getRecords();
		if (list == null || list.size() < 1) {
			throw new RuntimeException("已经是最后一条数据，无法下移");
		}
		EquipRepairStepEntity equipRepairStepEntity = list.get(0);

		int temp = selectById.getStepSequence();
		selectById.setStepSequence(equipRepairStepEntity.getStepSequence());
		equipRepairStepEntity.setStepSequence(temp);
		baseEntityDataUtil.updateBaseEntityData(selectById);
		baseEntityDataUtil.updateBaseEntityData(equipRepairStepEntity);

		ArrayList<EquipRepairStepEntity> arrayList = new ArrayList<EquipRepairStepEntity>();
		arrayList.add(equipRepairStepEntity);
		arrayList.add(selectById);

		boolean batchById = updateBatchById(arrayList);

		if (!batchById) {
			throw new RuntimeException("下移失败");
		}
		return resultBean;
	}

	// 查询
	public ResultDev selectByCondition(EquipRepairStep obj) {
		QueryWrapper<EquipRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairStepEntity>();
		queryWrapper.eq("row_status", 1);
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		if (obj != null && obj.getInnerData() != null) {
			if (obj.getInnerData().getRepairItem() != null && !"".equals(obj.getInnerData().getRepairItem())) {
				queryWrapper.like("repair_item", obj.getInnerData().getRepairItem());
			}
			if (obj.getInnerData().getRepairId() != 0) {
				queryWrapper.eq("repair_id", obj.getInnerData().getRepairId());
			}
		}
		queryWrapper.orderByAsc("step_sequence");
		Page<EquipRepairStepEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
		IPage<EquipRepairStepEntity> iPage = equipRepairStepDao.selectPage(page, queryWrapper);
		List<EquipRepairStepEntity> records = iPage.getRecords();
		if (records != null && records.size() > 0) {
			ArrayList<EquipRepairStep> listEquipRepairStep = new ArrayList<EquipRepairStep>();
			for (EquipRepairStepEntity item : records) {
				EquipRepairStep equipRepairStep = new EquipRepairStep();
				equipRepairStep.setInnerData(item);
				if (item.getAttachmentGroupId() != 0) {
					List<QualityAttachmentGroup> byAttachmentGroupId = qualityAttachmentGroupDataHandler
							.getByAttachmentGroupId(item.getAttachmentGroupId());
					equipRepairStep.setListAttachmentGroup(byAttachmentGroupId);
				}
				listEquipRepairStep.add(equipRepairStep);
			}
			ResultDev resultDev = new ResultDev();
			resultDev.setEntityList(listEquipRepairStep);
			resultDev.setTotalQuantity((int) iPage.getTotal());
			resultDev.setPageCount((int) iPage.getPages());
			return resultDev;

		}
		return null;

	}

}
